package com.ruoyi.smartcity.park.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.ruoyi.smartcity.park.domain.ParkChongzhi;
import com.ruoyi.smartcity.park.service.IParkChongzhiService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 充值信息Controller
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/chongzhi")
public class ParkChongzhiController extends BaseController {
    @Autowired
    private IParkChongzhiService parkChongzhiService;

    // 查询充值金额
    @GetMapping("/getUserAmount")
    public AjaxResult getUserAmount() {
        LoginUser user = SecurityUtils.getLoginUser();

        // 根据用户ID 查询充值金额
        ParkChongzhi item = parkChongzhiService.selectParkChongzhiByUserid(user.getUserid());
        if (item == null) {
            item = new ParkChongzhi();
            item.setUserid(user.getUserid());
            item.setUsername(user.getUsername());
            item.setAmount(new BigDecimal(0));
            parkChongzhiService.insertParkChongzhi(item);
        }
        return success(item);
    }

    /**
     * 查询充值信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ParkChongzhi parkChongzhi) {
        startPage();
        List<ParkChongzhi> list = parkChongzhiService.selectParkChongzhiList(parkChongzhi);
        return getDataTable(list);
    }

    /**
     * 导出充值信息列表
     */

    @Log(title = "充值信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParkChongzhi parkChongzhi) {
        List<ParkChongzhi> list = parkChongzhiService.selectParkChongzhiList(parkChongzhi);
        ExcelUtil<ParkChongzhi> util = new ExcelUtil<ParkChongzhi>(ParkChongzhi.class);
        util.exportExcel(response, list, "充值信息数据");
    }

    /**
     * 获取充值信息详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(parkChongzhiService.selectParkChongzhiById(id));
    }

    /**
     * 新增充值信息
     */

    @Log(title = "充值信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkChongzhi parkChongzhi) {
        return toAjax(parkChongzhiService.insertParkChongzhi(parkChongzhi));
    }

    /**
     * 修改充值信息
     */

    @Log(title = "充值信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkChongzhi parkChongzhi) {
        return toAjax(parkChongzhiService.updateParkChongzhi(parkChongzhi));
    }

    /**
     * 删除充值信息
     */

    @Log(title = "充值信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(parkChongzhiService.deleteParkChongzhiByIds(ids));
    }
}
